﻿using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using MiniJSON;

public class Server : MonoBehaviour 
{
	// TODO: on every action add overlay and disable all buttons while loading
	public UserPanel userPanel;

	string projectURL = "http://pixel-punch.com/Projects/Tests/actions.php";

	public void AddNewQuestion(string question, string answers)
	{
		StartCoroutine(_AddNewQuestion(question, answers));
	}

	public void StartResults(bool enable)
	{
		StartCoroutine(_StartResults(enable));
	}

	public void RefreshQuestion()
	{
		StartCoroutine(_RefreshQuestion());
	}

	public void SendVote(string question, string answer)
	{
		StartCoroutine(_SendVote(question, answer));
	}





	IEnumerator _AddNewQuestion(string question, string answers)
	{
		WWWForm form = new WWWForm();
		form.AddField("question", question);
		form.AddField("answers", answers);

		WWW www = new WWW(projectURL + "?type=new", form);

		yield return www;

		if (www.error != null)
		{
			Debug.LogError("WWW error(_AddNewQuestion): " + www.error);
			yield break;
		}

		else if (www.text.Length == 0)
		{
			Debug.LogWarning("WWW Warning(_AddNewQuestion): www.text returned empty");
			yield break;
		}

		Debug.Log("WWW Log(_AddNewQuestion): " + www.text + ", time: " + Time.time);
		//Debug.Log(www.text);
	}

	IEnumerator _StartResults(bool enable)
	{
		WWW www = new WWW(projectURL + "?type=results&enable=" + (enable ? "true" : "false"));

		yield return www;

		if (www.error != null)
		{
			Debug.LogError("WWW error(_StartResults): " + www.error);
			yield break;
		}

		else if (www.text.Length == 0)
		{
			Debug.LogWarning("WWW Warning(_StartResults): www.text returned empty");
			yield break;
		}

		Debug.Log("WWW Log(_StartResults): " + www.text + ", time: " + Time.time);
		//Debug.Log(www.text);
	}

	IEnumerator _RefreshQuestion()
	{
		WWW www = new WWW(projectURL + "?type=get_question");

		yield return www;

		if (www.error != null)
		{
			Debug.LogError("WWW error(_RefreshQuestion): " + www.error);
			yield break;
		}

		else if (www.text.Length == 0)
		{
			Debug.LogWarning("WWW Warning(_RefreshQuestion): www.text returned empty");
			yield break;
		}

		Dictionary<string, object> data = Json.Deserialize(www.text) as Dictionary<string, object>;
		string question = data["question"].ToString();
		string answers = data["answers"].ToString();
		userPanel.SetQuestion(question, answers);

		Debug.Log("WWW Log(_RefreshQuestion): " + www.text + ", time: " + Time.time);
		//Debug.Log(www.text);
	}

	IEnumerator _SendVote(string question, string answer)
	{
		WWWForm form = new WWWForm();
		form.AddField("question", question);
		form.AddField("answer", answer);
		form.AddField("device_id", SystemInfo.deviceUniqueIdentifier);

		WWW www = new WWW(projectURL + "?type=vote", form);

		yield return www;

		if (www.error != null)
		{
			Debug.LogError("WWW error(_SendVote): " + www.error);
			yield break;
		}

		else if (www.text.Length == 0)
		{
			Debug.LogWarning("WWW Warning(_SendVote): www.text returned empty");
			yield break;
		}

		Debug.Log("WWW Log(_SendVote): " + www.text + ", time: " + Time.time);
		//Debug.Log(www.text);
	}
}
